<!--
 * @description  :
 * @descriptionDetail:
 * @copyright    : 浙江烟草
 * @author       : mhb
 * @create       : 2023-01-17 11:06:06
-->
/**
 * @description  : H5地图导航
 * @descriptionDetail : H5地图导航
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-12-05 16:24:19
 */
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif -->
    <view class="content" :style="{width: getContainerWidth(), height: getContainerHeight()}" id="container">
    </view>
  </view>
</template>

<script>
import {
  AMapMixin,
} from '@/common/AMapMixin.js';
export default {
  onLoad() {
    this.pos = getApp().globalData.intentParams;
  },
  mixins: [AMapMixin,],
  data() {
    return {
      pos: undefined,
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.$modal.showLoading('地图加载中...');
    },
    initAmap() {
      let self = this;
      if (this.pos == undefined || this.pos.longitude == undefined) {
        this.$modal.hideLoading();
        this.$modal.alert('导航地点坐标未设置', () => {
          uni.navigateBack();
        });
        return;
      }
      this.getCurrentLocation().then(
        res => {
          self.setMapInstance([res.lng, res.lat,]);
        },
        err => {
          self.justInitMap();
        }
      );
    },
    getContainerHeight() {
      let p = (this.$u.sys().windowHeight - 44 - this.$u.sys().statusBarHeight) + "px";
      // #ifdef H5-DINGDING
      p = (this.$u.sys().windowHeight - this.$u.sys().statusBarHeight) + "px";
      // #endif
      return p;
    },
    getContainerWidth() {
      return this.$u.sys().windowWidth + "px";
    },
    setMapInstance(coords) {
      if (!this.hasInited) return;
      let self = this;
      self.curCords = new AMap.LngLat(coords[0], coords[1]);
      self.amap = new AMap.Map('container', {
        resizeEnable: true,
        zoom: 18,
      });
      this.$modal.hideLoading();
      let path = [];
      path.push([coords[0], coords[1],]);
      path.push([this.pos.longitude, this.pos.latitude,]);
      self.loadPlugin(["AMap.DragRoute", "AMap.ToolBar",],
        function() {
          self.amap.addControl(new AMap.ToolBar());
          let route = new AMap.DragRoute(self.amap, path, AMap.DrivingPolicy.LEAST_FEE); // 构造拖拽导航类
          route.search(); // 查询导航路径并开启拖拽导航
        }
      );
    },
    justInitMap() {
      this.$modal.alert("请在设备的设置中开启APP的定位权限后重新获取当前位置");
      this.amap = new AMap.Map('container', {
        resizeEnable: true,
        zoom: 18,
      });
      let self = this;
      this.$modal.hideLoading();
    },
  },
}
</script>

<style scoped lang="scss">
	uni-page-body {
		min-height: 100%;
		width: 100%;
		background-color: #F1F0F6;
	}
</style>
